import { resolve } from 'path';
import { defineConfig, externalizeDepsPlugin, loadEnv } from 'electron-vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite'; // 自动导入模块的 Vite 插件
import Components from 'unplugin-vue-components/vite'; // 自动导入 Vue 组件的 Vite 插件
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { fileURLToPath } from 'node:url';

export default defineConfig(({ command, mode }) => {
  // mode: 区分生产环境还是开发环境
  console.log('command, mode -> ', command, mode);
  const env = loadEnv(mode, fileURLToPath(new URL('./env', import.meta.url)));
  console.log('环境变量 env -> ', env);

  return {
    main: {
      plugins: [externalizeDepsPlugin()],
      build: {
        rollupOptions: {
          external: ['serialport'],
        },
      },
    },
    preload: {
      plugins: [externalizeDepsPlugin()],
    },
    renderer: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
        },
      },
      plugins: [vue(), AutoImport({ resolvers: [ElementPlusResolver()] }), Components({ resolvers: [ElementPlusResolver()] })],
      // 设置scss的api类型为modern-compiler
      css: {
        preprocessorOptions: {
          scss: {
            api: 'modern-compiler',
            // 消除一些不必要的警告
            silenceDeprecations: ['legacy-js-api'],
          },
        },
      },
    },
  };
});
